package com.sdy.common.utils;

import com.sdy.common.constant.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zzq on 2017/8/24.
 */
public class StringUtil extends StringUtils {

    public static boolean equals(Object cs1, Object cs2) {
        return StringUtils.equals((CharSequence) cs1, (CharSequence) cs2);
    }

    public static boolean isBlank(final Object cs) {
        return StringUtils.isBlank((CharSequence) cs);
    }

    public static boolean isNotBlank(final Object cs) {
        return StringUtils.isNotBlank((CharSequence) cs);
    }

    public static boolean isEmpty(final CharSequence cs) {
        return StringUtils.isEmpty(cs);
    }

    public static boolean isNotEmpty(final CharSequence cs) {
        return StringUtils.isNotEmpty(cs);
    }

    public static boolean isPhone(String phone) {
        if (phone == null) {
            return false;
        }
        return Pattern.matches(Constants.REGEX_PHONE, phone);
    }

    public static boolean isLoginname(String loginname) {
        if (loginname == null) {
            return false;
        }
        return Pattern.matches(Constants.REGEX_USERNAME, loginname);
    }

    public static String removeStr(String src, String rmstr) {
        return src.replace(rmstr, "");
    }

    public static String hideName(String name) {
        if (StringUtils.isBlank(name)) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < name.length(); i++) {
                if (i == 0) {
                    sb.append(name.charAt(i));
                } else {
                    sb.append("*");
                }
            }
            return sb.toString();
        }
    }

    public static Boolean regex(String regex, Object value) {
        if (value == null) {
            return false;
        }
        return Pattern.matches(regex, value.toString());
    }

    public static List<String> find(String regex, Object value, List<String>... params) {
        List<String> l = new ArrayList<>();
        if (value == null) {
            return l;
        }
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(value.toString());
        while (m.find()) {
            l.add(m.group());
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    params[i].add(m.group(i + 1));
                }
            }
        }
        return l;
    }

    public static String transform(String s) {
        return s == null ? "" : s;
    }

    /**
     * 校验合法字符串
     * @param w
     * @return
     */
    public static boolean checkValidWord(String w) {
        if (isBlank(w)) {
            return false;
        }
        return true;
    }

    /**
     * 去除字符串后面的空格
     */
    public static String ltrim(String v) {
        if (v == null) {
            return null;
        }
        int len = v.length(), st = 0;
        while (st < len && v.charAt(st) <= ' ') {
            st++;
        }
        return v.substring(st, len);
    }

    /**
     * 去除字符串后面的空格
     */
    public static String rtrim(String v) {
        if (v == null) {
            return null;
        }
        int t = v.length();
        while (t > 0 && v.charAt(t - 1) <= ' ') {
            t--;
        }
        return v.substring(0, t);
    }
}
